package com.yunhoo.contract.bean;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.checkFlow.factory.dao.BillCheck;
import com.yunhoo.checkFlow.factory.dao.CheckFlowFactory;
import com.yunhoo.checkFlow.factory.impl.BaseCheck;
import com.yunhoo.checkFlow.factory.impl.ContractCheckFlowFactory;
import com.yunhoo.checkFlow.model.FlowInfo;
import com.yunhoo.checkFlow.util.BillType;
import com.yunhoo.contract.db.impl.ContractImpl;
import com.yunhoo.contract.model.MainInformation;
import com.yunhoo.sys.db.util.IbatisTemplate;

import com.yunhoo.sys.util.common.*;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "contractCheckBean")
@SessionScoped
public class ContractCheckBean implements Serializable {

    private BillCheck check;
    //
    private final String billType = BillType.CONTRACT_INPUT;//合同录入 --> 审批流中类型
    private final String cpsn_number = LoginParam.getPer().getCpsn_number();//审批人id
    private final String loginRole = LoginParam.getPer().getRole();//审批人登录角色
    //
    private MainInformation detail = new MainInformation();//合同信息查看
    private LinkedList<MainInformation> dataList = new LinkedList<MainInformation>();//待审批合同列表
    //
    private FlowInfo currennode = new FlowInfo(true);//当前审批信息
    private List<FlowInfo> historyFlowInfo = null;//历史审批信息
    //
    private Integer rowIndex = -1;//当前 所在条目
    private boolean checkAble = false;// true：可审批，false: 不可审批
    private boolean cancelAble = false;// true: 可取消 false: 不可取消

    public ContractCheckBean() {
        initCheck();
        /* 初始化审批列表 */
        initList();
        /* 为标识审批状态的 billStatusForChecker 字段设值 */
        buildBillStatus();
    }

    public void queryList() {
        initList();
        /* 刷新列表 */
        check.refreshAllInfoMap();
        /* 为标识审批状态的 billStatusForChecker 字段设值 */
        buildBillStatus();
    }

    private void initList() {
        List<MainInformation> list = ContractImpl.queryContractListForCheck(null);
        dataList.clear();
        dataList.addAll(list);
    }

    private void initCheck() {
        CheckFlowFactory cff = new ContractCheckFlowFactory();
        check = cff.produceCheckFlow(billType, cpsn_number, loginRole);
    }

    private void buildBillStatus() {
        String billStatus;
        for (MainInformation temp : dataList) {
            /* 对处于审批流 审批中 的单据进行检测，判断当前审批人 是否可以审批 */
            billStatus = check.buildBillStatusForList(temp.getNumber(), temp.getFlowMark());
            temp.setBillStatusForChecker(billStatus);
        }
    }

    public void rowSelected(Integer rowKey) {
        rowIndex = rowKey;
        detail = dataList.get(rowIndex);

        initDetail();
        NavigationTool.getSelectedUrl("/page/contract/check/checkflowinfo.xhtml");
    }

    public void goBack() {
        NavigationTool.getSelectedUrl("/page/contract/check/check.xhtml");
    }

    private void refreshFlowMark() {
        MainInformation temp = ContractImpl.queryMainInfoByNumber(detail.getNumber());
        detail.setFlowMark(temp.getFlowMark());
        detail.setBillStatusForChecker(check.buildBillStatusForSingleBill(detail.getNumber(), detail.getFlowMark()));
    }

    /* 通过 */
    public void check() {
        boolean ok = false;

        String billNumber = detail.getNumber();//单据编号

        if ("1".equals(currennode.getIsConfirm())) {
            ok = check.pass(billNumber, currennode);

        } else if ("2".equals(currennode.getIsConfirm())) {
            ok = check.sendBack(billNumber, currennode);

        } else ToolKit.msgs_err("请选择审批意见");

        if (ok) {
            ToolKit.msgs_info("已审批");
            refreshFlowMark();
            initDetail();
        }
    }

    private boolean checkIfCancelAble() {
        int flag = check.checkIfCancelAble(detail.getNumber());
        if (flag == 1) return true;
        else return false;
    }

    //取消审批
    public void cancelCheck() {

        String billNumber = detail.getNumber();//单据编号

        int flag = check.cancelCheck(billNumber);

        if (flag == 1) {

            ToolKit.msgs_info("已取消");
            refreshFlowMark();
            initDetail();
        } else if (flag == -3) {

            ToolKit.msgs_info("请先审批");
        } else if (flag == -2) {

            ToolKit.msgs_info("已有其他人进行了审批，无法取消");
        } else if (flag == -1) {

            ToolKit.msgs_info("数据库异常，取消失败");
        }
    }

    public void first() {
        if (!Null.check(dataList)) {
            rowIndex = 0;
            detail = dataList.getFirst();
            initDetail();
        } else {
            ToolKit.msgs_err("没有数据!");
        }
    }

    public void last() {
        if (!Null.check(dataList)) {
            rowIndex = dataList.size() - 1;
            detail = dataList.getLast();
            initDetail();
        } else {
            ToolKit.msgs_err("没有数据!");
        }
    }

    public void prev() {
        if (rowIndex > 0) {
            rowIndex--;
            detail = dataList.get(rowIndex);
            initDetail();
        } else {
            ToolKit.msgs_err("没有了!");
        }
    }

    public void next() {
        if (rowIndex > -1 && rowIndex < dataList.size() - 1) {
            rowIndex++;
            detail = dataList.get(rowIndex);
            initDetail();
        } else {
            ToolKit.msgs_err("没有了!");
        }
    }

    private void initDetail() {
        /* 历史审批信息 */
        historyFlowInfo = check.historyFlowInfo(detail.getNumber());

        /* 检查当前节点是否可审批 */
        detail.setBillStatusForChecker(check.buildBillStatusForSingleBill(detail.getNumber(), detail.getFlowMark()));

        if ("0".equals(detail.getBillStatusForChecker())) checkAble = true;
        else {
            checkAble = false;
            cancelAble = checkIfCancelAble();
        }
    }

    public void contentListener() {
        if (currennode.getIsConfirm().equals("1")) {
            currennode.setContent("同意");
        }
        if (currennode.getIsConfirm().equals("2")) {
            currennode.setContent("不同意");
        }
    }

    public MainInformation getDetail() {
        return detail;
    }

    public void setDetail(MainInformation detail) {
        this.detail = detail;
    }

    public LinkedList<MainInformation> getDataList() {
        return dataList;
    }

    public void setDataList(LinkedList<MainInformation> dataList) {
        this.dataList = dataList;
    }

    public FlowInfo getCurrennode() {
        return currennode;
    }

    public void setCurrennode(FlowInfo currennode) {
        this.currennode = currennode;
    }

    public List<FlowInfo> getHistoryFlowInfo() {
        return historyFlowInfo;
    }

    public void setHistoryFlowInfo(List<FlowInfo> historyFlowInfo) {
        this.historyFlowInfo = historyFlowInfo;
    }

    public Integer getRowIndex() {
        return rowIndex;
    }

    public void setRowIndex(Integer rowIndex) {
        this.rowIndex = rowIndex;
    }

    public boolean isCheckAble() {
        return checkAble;
    }

    public void setCheckAble(boolean checkAble) {
        this.checkAble = checkAble;
    }

    public boolean isCancelAble() {
        return cancelAble;
    }

    public void setCancelAble(boolean cancelAble) {
        this.cancelAble = cancelAble;
    }
}
